var app = getApp();
var util = require('../../utils/util.js');

Page({
  data: {
    imgPath: '../../images/wx.png',
    // region: ['北京市', '北京市', '东城区'],
    selectedIds: [65112, 65113, 65114],
    multiArray: [],
    multiIndex: [0, 0],
    multiArrayOne: [],
    multiArrayTwo: [],
    multiArrayIdOne: [],
    multiArrayIdTwo: [],
    nowDate: util.getNowFormatDate(),
    date: util.getNowFormatDate(),
    ampm: '上午',
    array: ['上午', '下午'],
    objectArray: [
      {
        id: 0,
        name: '上午'
      },
      {
        id: 1,
        name: '下午'
      }
    ],
    index: 0,
    morningOrAfter: 0,
    number: 0,
    tipwords: ''
  },
  onLoad: function (options) {
    var that = this;
    var reqParams = {
      province: that.data.selectedIds[0].toString(),
      city: that.data.selectedIds[1].toString(),
      area: that.data.selectedIds[2].toString(),
      createTime: that.data.date,
      morningOrAfter: that.data.morningOrAfter
    }
    this.getRequestData(reqParams); // 获取剩余人数
    this.getTipsWord(); // 获取提示语
    // 获取区县
    wx.request({
      url: app.globalData.apiBaseUrl + '/city/getArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      data: { id: 65112 },
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          var districtOneArr = res.data.data
          var multiArrayOne = districtOneArr.map((item, index, arr) => {
            return item.name
          })
          var multiArrayIdOne = districtOneArr.map((item, index, arr) => {
            return item.id
          })
          that.setData({
            multiArrayOne: multiArrayOne,
            multiArrayIdOne: multiArrayIdOne
          })
          that.getTwo(multiArrayIdOne[0]);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
          setTimeout(function () {
            wx.hideToast()
          }, 3000);
        }
      }
    })
  },
  bindMultiPickerChange: function (e) {
    var that = this;
    var idsOne = this.data.multiArrayIdOne[this.data.multiIndex[0]];
    var idsTwo = this.data.multiArrayIdTwo[this.data.multiIndex[1]];
    this.setData({
      multiIndex: e.detail.value,
      selectedIds: [65112, idsOne, idsTwo]
    })
    // console.log(this.data.selectedIds)
    this.getRequestData({
      province: that.data.selectedIds[0].toString(),
      city: that.data.selectedIds[1].toString(),
      area: that.data.selectedIds[2].toString(),
      createTime: that.data.date,
      morningOrAfter: that.data.morningOrAfter
    }); // 获取剩余人数
  },
  bindMultiPickerColumnChange: function (e) {
    var that = this;
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex,
      multiArrayIdTwo: this.data.multiArrayIdTwo
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    var id_session = this.data.multiArrayIdOne[0];
    switch (e.detail.column) {
      case 0:
        var multiArrayIdOne = this.data.multiArrayIdOne;
        var paramsId = multiArrayIdOne[e.detail.value];
        that.getTwo(paramsId);
        data.multiIndex[1] = 0;
        break;
    }
    this.setData(data);
  },
  getTwo: function (parentId) {
    var that = this;
    wx.request({
      url: app.globalData.apiBaseUrl + '/city/getArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      data: { id: parentId },
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          var districtTwoArr = res.data.data
          var multiArrayTwo = districtTwoArr.map((item, index, arr) => {
            return item.name
          })
          var multiArrayIdTwo = districtTwoArr.map((item, index, arr) => {
            return item.id
          })
          that.setData({
            multiArrayTwo: multiArrayTwo,
            multiArrayIdTwo: multiArrayIdTwo
          })
          var arrOne = that.data.multiArrayOne;
          var arrTwo = that.data.multiArrayTwo;
          that.setData({
            multiArray: [arrOne, arrTwo]
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
          setTimeout(function () {
            wx.hideToast()
          }, 3000);
        }
      }
    })
  },
  // 选择上午/下午
  bindPickerChange(e) {
    var that = this;
    this.setData({
      index: e.detail.value,
      morningOrAfter: e.detail.value
    });
    this.getRequestData({
      province: that.data.selectedIds[0].toString(),
      city: that.data.selectedIds[1].toString(),
      area: that.data.selectedIds[2].toString(),
      createTime: that.data.date,
      morningOrAfter: that.data.morningOrAfter
    }); // 获取剩余人数
    console.log(this.data.morningOrAfter)
  },
  // bindRegionChange(e) {
  //   var that = this;
  //   this.setData({
  //     region: e.detail.value
  //   })
  //   this.getRequestData({
  //     province: that.data.region[0],
  //     city: that.data.region[1],
  //     area: that.data.region[2],
  //     createTime: that.data.date,
  //     morningOrAfter: that.data.morningOrAfter
  //   }); // 获取剩余人数
  // },
  bindDateChange: function (e) {
    var that = this;
    this.setData({
      date: e.detail.value
    });
    this.getRequestData({
      province: that.data.selectedIds[0].toString(),
      city: that.data.selectedIds[1].toString(),
      area: that.data.selectedIds[2].toString(),
      createTime: that.data.date,
      morningOrAfter: that.data.morningOrAfter
    }); // 获取剩余人数
  },
  changAmpm: function(event) {
    var that = this;
    var thisVal = event._relatedInfo.anchorTargetText;
    if (thisVal == '上午') {
      this.setData({
        ampm: '下午',
        morningOrAfter: 1
      })
      this.getRequestData({
        province: that.data.selectedIds[0].toString(),
        city: that.data.selectedIds[1].toString(),
        area: that.data.selectedIds[2].toString(),
        createTime: that.data.date,
        morningOrAfter: that.data.morningOrAfter
      }); // 获取剩余人数
    } else {
      this.setData({
        ampm: '上午',
        morningOrAfter: 0
      })
      this.getRequestData({
        province: that.data.selectedIds[0].toString(),
        city: that.data.selectedIds[1].toString(),
        area: that.data.selectedIds[2].toString(),
        createTime: that.data.date,
        morningOrAfter: that.data.morningOrAfter
      }); // 获取剩余人数
    }
  },
  goAppointInfo: function() {
    var that = this;
    if (this.data.number < 0 || this.data.number == 0) {
      wx.showToast({
        title: '预约名额已满',
        icon: 'none'
      })
    } else if (this.data.date < this.data.nowDate) {
      wx.showToast({
        title: '请选择正确的预约时间',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: "../appointmentForm/appointmentForm?date=" + that.data.date + "&morningOrAfter=" + that.data.morningOrAfter
      })
    }
    setTimeout(function () {
      wx.hideToast()
    }, 3000)
  },
  getRequestData: function (params) {
    var that = this;
    // 获取剩余人数
    wx.request({
      url: app.globalData.apiBaseUrl + '/studentOrder/getOrderCount',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      data: params,
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          if (res.data.data.residue < 0) {
            that.setData({
              number: 0
            })
          } else {
            that.setData({
              number: res.data.data.residue
            })
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
    setTimeout(function() {
      wx.hideToast()
    }, 3000)
  },
  // 获取提示语
  getTipsWord: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiBaseUrl + '/homePageSetting/getSystemSetting',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            // imgPath: app.globalData.apiBaseUrl + '/file/downLoadPhotos?path=' + res.data.data.imgPath,
            imgPath: res.data.data.imgPath,
            tipwords: res.data.data.primaryNote
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
    setTimeout(function () {
      wx.hideToast()
    }, 3000)
  }
})